rm(list=ls())

library(dplyr)
library(ggplot2)
library(ggthemes)
library(tidyverse)

################
#Computing specifications: 
################
# the codes below are implemented in the following software versions: 
## RStudio 2022.07.1+554 "Spotted Wakerobin" Release (7872775ebddc40635780ca1ed238934c3345c5de, 2022-07-22) for Windows Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) QtWebEngine/5.12.8 Chrome/69.0.3497.128 Safari/537.36
## R version 4.2.0 (2022-04-22 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19044)
## tidyverse_1.3.1
## ggplot2_3.3.6
# correct versions of tidyverse and ggplot2 may be needed to replicate figure 5 and figure 6

load('sentiment_tiles.RData')
undergrad_predicted <- read.csv('kw_relevance_predicted_set.csv', stringsAsFactors = FALSE)
load('distances.RData')
data <- data %>%  select(country_1, year, self_mention)
data <- unique(data)

tiles <- data %>% select(country_1, year, self_mention) %>% right_join(tiles, by=c('country_1'='country', 'year'))

tiles <- tiles[undergrad_predicted$Predicted == 1,]
undergrad_predicted <- undergrad_predicted %>% filter(Predicted == 1)

to_plot <- tiles %>% group_by(year, country_1) %>% dplyr::summarise(mean_predicted_sentiment = mean(predicted_sentiment), mean_google_sentiment = mean(google_sentiment_score)) #%>% group_by(year) %>% summarise(mean_google_sentiment = mean(mean_google_sentiment, mean_predicted_sentiment = mean(mean_predicted_sentiment)))

# Figure 5: compare against all sentiment by year
all_sentiment <- read_csv('UNGA_sentiment_complete.csv')
all_sentiment_years <- all_sentiment %>% group_by(year, country) %>% dplyr::summarise(average_sentiment = mean(sentiment_score)) #%>% group_by(year) %>% summarise(average_sentiment = mean(average_sentiment))

# 600x450
gg <- ggplot(NULL) + geom_smooth(aes(x=to_plot$year, y=to_plot$mean_google_sentiment), color='red') + 
  geom_smooth(aes(x=all_sentiment_years$year, y=all_sentiment_years$average_sentiment)) + 
  theme_tufte() +  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), axis.line = element_line(colour = "black"))+
  xlab(NULL) + ylab('Mean Google Sentiment') + 
  annotate('text', x=2010, y = 0.16, label='All Speeches', color='blue') + 
  annotate('text', x=2010, y = 0.07, label='Borders Only', color='red')

ggsave(gg, file='Figure 5_new.png', width=5.5, height=4)


#figure 6
global_north <- c('AND', 'ALB', 'AUS', 'AUT', 'BIH', 'BEL', 'BGR', 'BMU', 'CAN', 'CHE', 'CYP', 'CZE', 'DEU', 'DNK', 'EST', 'ESP', 'FIN',
                  'FRO', 'FRA', 'GBR', 'GIB', 'GRL', 'GRC', 'HKG', 'HRV', 'HUN', 'IRE', 'ISL', 'ISR', 'ITA', 'JPN', 'KOR', 'LIE', 'LTU',
                  'LUX', 'LAT', 'MCO', 'MDA', 'MKD', 'MAC', 'MLT', 'NLD', 'NOR', 'NZL', 'POL', 'SPM', 'PRT', 'ROU', 'RUS', 'SWE', 'SVN',
                  'SJM', 'SVK', 'SMR', 'TUR', 'TWN', 'USA', 'VAT', 'SRB', 'MNE', 'ALA', 'GGY', 'IMN', 'JEY')

tiles$region <- ifelse(tiles$country %in% global_north, 'North', 'South')

gg <- ggplot(tiles, aes(year, predicted_sentiment, color=region)) + geom_smooth(alpha = 0.5, fill='grey85') + 
  xlab(NULL) + ylab('Mean Predicted Sentiment') +
  theme_tufte() +  theme(legend.position = 'none',
                         panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), axis.line = element_line(colour = "black")) +
  scale_color_manual(values=c('red', 'blue')) + 
  annotate('text', x=1998, y = 0.2, label='North', color='red') + 
  annotate('text', x=1998, y = -0.1, label='South', color='blue')
  



